package com.supermap.application.service;

import com.supermap.application.dao.BaseAppDao;
import com.supermap.application.dao.BaseDaoImpl;
import com.supermap.application.dao.MacroTablemetaDao;
import com.supermap.application.entity.TMacroIdent;
import com.supermap.application.entity.TMacroTablemeta;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName MacroDataImportService
 * @Description TODO
 * @Author HEKE
 * @Date 2019/2/25 17:48
 */
@Service
public class MacroDataImportService extends BaseService {

    /** 实体管理 */
    @Autowired
    @PersistenceContext
    private EntityManager entityManager;

    @Autowired
    private MacroTablemetaDao macroTablemetaDao;

    /**
     * 获取当前数据对应的分组
     * @param idencode
     * @return
     */
    public List<TMacroIdent> getIdentByIdenCode(String idencode){
        List<TMacroIdent> results = new ArrayList<>();
        if(idencode == null || idencode == ""){
            return results;
        }
        String sql = "  select distinct i.maitid,i.name from (select g.maitid\n" +
                "     from (select e.maivid\n" +
                "             from (select c.iden_code\n" +
               "                     from (select a.matmid\n" +
                "                             from t_macro_tablemeta a\n" +
                "                            where a.iden_code = '"+idencode+"' ) b,\n" +
                "                          t_macro_tableinfo c\n" +
                "                    where c.parid = b.matmid) d,\n" +
                "                  t_macro_idenmeta e\n" +
                "            where e.iden_code = d.iden_code) f,\n" +
                "          t_macro_idenvl g\n" +
                "    where g.maivid = f.maivid) h,t_macro_ident i where i.maitid = h.maitid order by i.maitid\n";
        BaseDaoImpl<TMacroTablemeta,Integer> dao = new BaseDaoImpl<>(TMacroTablemeta.class,entityManager);
        List listDatas = dao.query(sql);
        if(listDatas == null || listDatas.size() < 1){
            return results;
        }else {
            //将数据格式化一下方便查看
            TMacroIdent tMacroIdent = null;
            for(int i = 0; i < listDatas.size() ; i ++){
                Object[] objs = (Object[]) listDatas.get(i);
                tMacroIdent = new TMacroIdent();
                tMacroIdent.setMaitid(Integer.parseInt(objs[0]+""));
                tMacroIdent.setName(objs[1] + "");
                results.add(tMacroIdent);
            }
        }
        return results;
    }
}
